﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using The_Square_Saga.Utils;

namespace The_Square_Saga.Objects
{
    abstract class PhysicsGameEntity : MovableGameEntity
    {

        /// <summary>
        /// the mass, between 0 and 1
        /// </summary>
        private float mass;
        public float Mass
        {
            get { return mass; }
            set 
            {
                if (value > 1)
                {
                    value = 1;
                }
                else if (value < 0)
                {
                    value = 0;
                }
                mass = value;  
            }
        }

        public new Rectangle CollisionRect 
        {
            get
            {
                if (isAnimated)
                {
                    return new Rectangle((int)position.X, (int)position.Y, (int)this.getCurrentAnimation().getFrameSize().X, (int)this.getCurrentAnimation().getFrameSize().Y);
                }
                else
                {
                    return new Rectangle((int)position.X, (int)position.Y, getImage().Width-1, getImage().Height-1);
                }
            }
        }

        /// <summary>
        /// If the player is grounded
        /// </summary>
        private bool isGrounded = false;

        public PhysicsGameEntity(Texture2D image, Vector2 position, Vector2 velocity, bool isAnimated)
            : base(image, position, velocity, isAnimated)
        {
            this.Velocity = new Vector2(velocity.X, Physics.GRAVITY);
        }

        /// <summary>
        /// Check collision between two physics object
        /// </summary>
        /// <param name="pGameEntity">An physics game entity</param>
        /// <returns>True if collide</returns>
        public bool isCollide(PhysicsGameEntity pGameEntity)
        {
            return this.CollisionRect.Intersects(pGameEntity.CollisionRect);
        }

        public bool IsGrounded()
        {
            return isGrounded;
        }

        public virtual void setGrounded(bool b)
        {
            this.isGrounded = b;
        }

        /// <summary>
        /// Apply gravity on this object
        /// </summary>
        public void applyGravity(GameTime gameTime)
        {
            if (this.mass > 0)
            {
                this.position.Y += Velocity.Y;
                this.Velocity =
                    new Vector2(this.Velocity.X, this.Velocity.Y + Physics.GRAVITY);
            }
        }
    }
}
